/**
 * Copyright (c) 2015-2030 by Beijing Yanlight Co., Ltd.
 * All rights reserved.
 */
package com.yanlight.app.module.partner.controller;

import com.yanlight.app.module.partner.request.*;
import com.yanlight.app.module.partner.service.PartnMerchantService;
import com.yanlight.support.http.Response;
import com.yanlight.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author haolw
 * @time 2018/07/04
 */
@RestController
@RequestMapping("/partn/mch")
public class PartnMerchantController {
    @Autowired
    private PartnMerchantService merchantService;


    @PostMapping("/detail")
    public Response detail() {
        Integer userId = SessionUtil.getUserId();
        return ResponseUtil.success(merchantService.findDetail(userId));
    }

    @PostMapping("/paycodes")
    public Response getPaycodes(MerchantPaycodesQueryRequest request) {
        Integer userId = SessionUtil.getUserId();
        request.setUserId(userId);
        return ResponseUtil.success(merchantService.findPagePaycodes(request));
    }

    @PostMapping("/getAvailablePaycodes")
    public Response getAvailablePaycodes() {
        Integer userId = SessionUtil.getUserId();
        return ResponseUtil.success(merchantService.findAvailablePaycodes(userId));
    }

    @PostMapping("/modifyPaycode")
    public Response modifyPaycode(MerchantPaycodeModifyRequest request) {
        merchantService.modifyPaycode(request);
        return ResponseUtil.success();
    }

    @PostMapping("/addPaycode")
    public Response addPaycode(MerchantPaycodeAddRequest request) {
        Integer userId = SessionUtil.getUserId();
        request.setUserId(userId);
        merchantService.addPaycode(request);
        return ResponseUtil.success();
    }

    @PostMapping("/getPaycode")
    public Response getPaycode(Integer id) {
        return ResponseUtil.success(merchantService.findPaycode(id));
    }

    @PostMapping("/subList")
    public Response subList(MerchantSubPageQueryRequest request) {
        Integer userId = SessionUtil.getUserId();
        request.setUserId(userId);
        return ResponseUtil.success(merchantService.findPageSubs(request));
    }

    @PostMapping("/addSub")
    public Response addSub(MerchantSubAddRequest request) {
        Integer userId = SessionUtil.getUserId();
        request.setUserId(userId);
        merchantService.addSub(request);
        return ResponseUtil.success();
    }

    @PostMapping("/modifySub")
    public Response modifySub(MerchantSubModifyRequest request) {
        merchantService.modifySub(request);
        return ResponseUtil.success();
    }

    @PostMapping("/getSubDetail")
    public Response findSubDetail(Integer id) {
        return ResponseUtil.success(merchantService.findSubDetail(id));
    }

    @GetMapping("/unifiedQRcode.jpg")
    public void getQR(String paycode, Integer rate, HttpServletResponse response) {
        String code = EncryptUtil.decodeBase64(paycode);

        PayQRcodeUtil.creatQRImage(code, rate, response);
        return;
    }

    @GetMapping("/downloadQR.jpg")
    public void downloadQR(String paycode, Integer rate, String name, String type, HttpServletResponse response) {
        String code = EncryptUtil.decodeBase64(paycode);

        PayQRcodeUtil.createDownloadQRImage(code, rate, name, type, response);
        return;
    }

    @RequestMapping("/bind")
    @ResponseBody
    public Response bind(Integer codeId, Integer companyId) {
        Response response = merchantService.bindCompany(codeId, companyId);
        return response;
    }
}
